// ref: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/IAM.html#attachRolePolicy-property
const AWS = require('aws-sdk');
const regions = require("../../common/awsRegions");
AWS.config.update({region: regions.region_ap_southeast_1});
const iam = new AWS.IAM({apiVersion: '2010-05-08'});

const d = {};

d.createRole = (params) => new Promise((resolve, reject) => {
  iam.createRole(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.getRole = (params) => new Promise((resolve, reject) => {
  iam.getRole(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.getPolicy = (params) => new Promise((resolve, reject) => {
  iam.getPolicy(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.attachRolePolicy = (params) => new Promise((resolve, reject) => {
  iam.attachRolePolicy(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

module.exports = d;
if (require.main === module) {
  (async () => {
    const getRoleTest = await module.exports.getRole();
    // const abc = await module.exports.deleteInstanceProfile({InstanceProfileName: "ecsInstanceRole"});
    // console.log(abc);
  })();
}